// pages/order/list.js
Page({
    data: {
      activeTab: 0,
      tabs: [
        { name: '全部', status: 'all', count: 0 },
        { name: '待付款', status: 'unpaid', count: 0 },
        { name: '待使用', status: 'delivering', count: 0 },
        { name: '已使用', status: 'completed', count: 0 }
      ],
      statusMap: {
        unpaid: '待付款',
        delivering: '待使用',
        completed: '已使用'
      },
      allOrders: [],
      filteredOrders: [],
      page: 1,
      loading: false
    },
  
    onLoad() {
      this.loadOrders()
    },
  
    // 加载订单数据
    async loadOrders() {
      this.setData({ loading: true })
      
      try {
        const res = await wx.request({
          url: 'https://api.example.com/orders',
          data: { page: this.data.page }
        })
        
        const orders = res.data.list
        this.setData({
          allOrders: [...this.data.allOrders, ...orders],
          [`tabs[0].count`]: res.data.total,
          [`tabs[1].count`]: res.data.unpaidCount,
          [`tabs[2].count`]: res.data.deliveringCount,
          [`tabs[3].count`]: res.data.completedCount
        })
        this.filterOrders()
      } finally {
        this.setData({ loading: false })
      }
    },
  
    // 过滤订单
    filterOrders() {
      const { activeTab, tabs, allOrders } = this.data
      const status = tabs[activeTab].status
  
      this.setData({
        filteredOrders: allOrders.filter(order => 
          status === 'all' ? true : order.status === status
        )
      })
    },
  
    // 切换选项卡
    switchTab(e) {
      const index = e.currentTarget.dataset.index
      this.setData({ activeTab: index }, () => {
        this.filterOrders()
      })
    },
  
    // 加载更多
    loadMore() {
      if (!this.data.loading) {
        this.setData({ page: this.data.page + 1 }, () => {
          this.loadOrders()
        })
      }
    },
  
    // 支付订单
    handlePay(e) {
      const orderNo = e.currentTarget.dataset.orderno
      wx.requestPayment({
   
      })
    },
  
    // 确认使用
    confirmReceipt(e) {
      const orderNo = e.currentTarget.dataset.orderno
      wx.showModal({
        title: '确认使用',
        content: '请确认您已使用商品',
        success: (res) => {
          if (res.confirm) {
            // 调用确认使用接口
          }
        }
      })
    }
  })